//
//  GGScheduleVC.swift
//  DongBaoDemo
//
//  Created by 陈志勇 on 2018/8/5.
//  Copyright © 2018年 陈志勇. All rights reserved.
//

import UIKit

class GGScheduleVC: GGBaseTableVC {
    var scheduleList: [Schedule] = [] {
        didSet {
            tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(UINib.init(nibName: "GGMatchesScheduleCell", bundle: nil), forCellReuseIdentifier: "GGMatchesScheduleCell")
        
        reqtuestRaceList()
    }
    
}

extension GGScheduleVC {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return scheduleList.count
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "GGMatchesScheduleCell", for: indexPath) as! GGMatchesScheduleCell
        cell.schedule = scheduleList[indexPath.row]
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        tableView.deselectRow(at: indexPath, animated: true)
        let schedule = scheduleList[indexPath.row]
        let vc = GGRaceDetailVC(schedule: schedule)
        navigationController?.pushViewController(vc, animated: true)
    }
    
}

// MARK: - 网络请求
extension GGScheduleVC {
    func reqtuestRaceList() {
        NetWorkTool.shared.loadtScheduleList{ (response) in
            
            
            guard let raceList = ResponseRaceList<Schedule>.deserialize(from: response.data?.info) else {
                return
            }
            
            guard let list = raceList.list else {
                return
            }
            
            self.scheduleList = list
        }
    }
}
